<template>
  <div id="app">
    <loading v-model="this.$store.state.isLoading"></loading>
    <router-view v-if="isRouterAlive"></router-view>
  </div>
</template>


 
<script>
import { mapState } from 'vuex'
export default {
  name: 'App',
  provide (){
     return {
       reload:this.reload,
       Autologin:this.Autologin,
       iflogin:this.iflogin,
     }
  },
  data(){
    return {
       isRouterAlive:true,
    }
  },
  methods:{
    reload (){
       this.isRouterAlive = false
       this.$nextTick(function(){
       this.isRouterAlive = true
       })
    },
    iflogin(user){
    if(!user){
        //跳去登陆
        this.$router.push('/Login');
        this.$vux.toast.show({
        type: 'text',
        text: '请先登录！',
        position: 'middle',
        time: '1000',
        })
    }
    },
    Autologin(){
    let User_id = sessionStorage.getItem('User_id');
    if(!User_id){
      // this.$router.push('/Post');
        this.$vux.toast.show({
        type: 'text',
        text: '请先登录！',
        position: 'middle',
        time: '1000',
        })
    }else{
        this.$store.state.wd.wdid = User_id;
        this.$http.post("http://localhost:3000/users/Autologin",
        {
        User_id:User_id,
        },{emulateJSON: true})
        .then(
        (response)=>{
          if(response.data.msg.status=='true'){
          this.$store.state.wd.wdid=response.data.msg.user.User_id;
          this.$store.state.wd.wdname=response.data.msg.user.User_name;
          this.$store.state.wd.wdtx=response.data.msg.user.User_tx;
          this.$store.state.wd.wdsex=response.data.msg.user.User_sex;
          this.$store.state.wd.wdlike=response.data.msg.user.Like;
          this.$store.state.wd.wdintro=response.data.msg.user.User_intro;

          this.$http.get("http://localhost:3000/post/Userpost/" + this.$store.state.wd.wdid)//帖子条数
          .then(
          (response)=>{
              this.$store.state.wd.wdtz=response.data.msg.utz.length.toString();
          },
          (error)=>{
          console.log("err");
            });

          this.$http.get("http://localhost:3000/sd/Usersd/" + this.$store.state.wd.wdid)//树洞条数
          .then(
          (response)=>{
              this.$store.state.wd.wdsd=response.data.utz.length.toString();
          },
          (error)=>{
          console.log("err");
            });

          this.$http.get("http://localhost:3000/pl/Userpl/" + this.$store.state.wd.wdid)//评论条数
          .then(
          (response)=>{
              this.$store.state.wd.wdpl=response.data.msg.pls.length.toString();
          },
          (error)=>{
          console.log("err");
            });

          this.$store.state.wd.wdlook=response.data.msg.user.User_look.length.toString();
          this.$store.state.wd.looklist=response.data.msg.user.User_look;//浏览列
          this.$store.state.wd.wddz=response.data.msg.user.User_dz.length.toString();
          this.$store.state.wd.dzlist=response.data.msg.user.User_dz;
          this.$store.state.wd.wdflow=response.data.msg.user.Flow;
          this.$store.state.wd.wdfriend=response.data.msg.user.Friend;
          this.$socket.emit('new_user', response.data.msg.user.User_id);//登录socket
        }
        },
        (error)=>{
        console.log(error);
        }
        );
    }
  },
  },
  created(){
    this.Autologin();
  },
  sockets:{
        // connect:function() {//与socket.io连接后回调
        // // this.$socket.emit('new_user', this.$store.state.user.name);// 请求加入
        // },

        login:function(value) {// 第一次登陆接收其它成员信息
        for(var i = 0; i < value.length; i++){
        this.$store.state.userlist.push(value[i]);
        }
        },

        user_joined:function (tid, index) {// 监听中途的成员加入
        if(!(tid in this.$store.state.userlist)){
        this.$store.state.userlist.push(tid);
        }
        },

        user_left:function (data) {// 监听中途的成员离开
        var index =this.$store.state.userlist.indexOf(data);
        this.$store.state.userlist.splice(index,1);
        },

        // receive_private_message:function (data) {// 接收私聊信息
        // this.$store.state.badge=parseInt(this.$store.state.badge+1)
        // },
        },
}
</script>


<style>
* {
    margin: 0;
    padding: 0;
}
</style>
